/**
 * constructor of class popUpPrice which is used to popup the price details.
 * @param array priceList an array with object inside
 */
/*
object : {
    'day' : the unit to count price,
    'price' : the cost,
    'avgClicks' : the average clicks,
    'avgCostPerClick' : the average cost per click
}
*/

/**
 * Show the space price
 * @param allSpacePrices hold all the spaces price
 */
var popUpPrice = function() {
    this.pricingBlock = document.getElementById('pricePreviewBlock');
    this.pricingTable = document.getElementById('adspacePricing');
    this.isDisplay = false;
    this.adjustion = null;
    this.allSpacePrices = null;
}

/**
 * Set the data of local variable allSpacePrice
 * @param {Array} priceList
 * @return void
 */
popUpPrice.prototype.setData = function(priceList) {
    this.allSpacePrices = priceList;
}

/**
 * Show the price preview
 * @param {Object} spaceId
 * @return void
 */
popUpPrice.prototype.show = function(spaceId) {
    this.hide();
    var spacePrice = this.allSpacePrices[spaceId];
    if (spacePrice) {
        for (var i = 0; i < spacePrice.length; i++) {
            var priceRow = document.createElement('tr');
            priceRow.style.textAlign = 'center';
            priceRow.style.backgroundColor = '#FFFFFF';
            var days = document.createElement('td');
            days.innerHTML = spacePrice[i].day;
            priceRow.appendChild(days);
            var avgClicks = document.createElement('td');
            avgClicks.innerHTML = spacePrice[i].avgClicks;
            priceRow.appendChild(avgClicks);
            var price = document.createElement('td');
            price.innerHTML = spacePrice[i].price;
            priceRow.appendChild(price);
            var avgCostPerClick = document.createElement('td');
            avgCostPerClick.innerHTML = spacePrice[i].avgCostPerClick;
            priceRow.appendChild(avgCostPerClick);
        
            if (!!document.all){
                this.pricingTable.childNodes[0].appendChild(priceRow);
            }
            else {
                this.pricingTable.childNodes[1].appendChild(priceRow);
            }
        }
    }

    this.isDisplay = true;
    this.pricingBlock.style.display = '';
}

/**
 * Hide the price preview
 * @return void
 */
popUpPrice.prototype.hide = function() {
    var child = (!!document.all) ? 
        this.pricingTable.childNodes[0] 
        : this.pricingTable.childNodes[1];
    var nodeLength = child.childNodes.length;
    for (var i = 1; i < nodeLength; i++) {
        child.removeChild(child.lastChild);
    }
    
    this.isDisplay = false;
    this.pricingBlock.style.display = 'none';
}

/**
 * Sets the popup position.
 * @param int top The top coordinate for the iframe.
 * @param int left The left coordinate for the iframe.
 * @return void
 */
popUpPrice.prototype.changePosition = function(top, left) {
    this.pricingBlock.style.top = parseInt(top) + 'px';
    this.pricingBlock.style.left = parseInt(left) + 'px';
};

/**
 * Adjust popup iframe position to locate within browser window.
 * @param array position The posotion of the source element
 * @param object element the source element
 */
popUpPrice.prototype.adjustPopUpPosition = function(position, element) {
    this.setSrcElement(element);
    var frameWidth = this.pricingBlock.offsetWidth;
    var frameHeight = this.pricingBlock.offsetHeight;
    var eLeft = position.left;
    var eTop = position.top;
    var bodyHeight = document.documentElement.clientHeight;
    var bodyWidth = document.documentElement.clientWidth;
    var scrollBarTop = document.documentElement.scrollTop;
    var scrollBarLeft = document.documentElement.scrollLeft;
    var browserHeight = bodyHeight + scrollBarTop;
    var browserWidth = bodyWidth + scrollBarLeft;
    var adjustionHeight = this.adjustion.height;
    var adjustionWidth = this.adjustion.width;
    if ((browserWidth < (eLeft + frameWidth)) && (eLeft - frameWidth - adjustionWidth) > 0) { //adjust herizontal position
        eLeft = eLeft - this.srcElement.offsetWidth - frameWidth;
        eLeft = Math.max(scrollBarLeft, eLeft);
    }

    if (browserHeight < (eTop + frameHeight)) { //adjust vertical position
        if ((eTop - scrollBarTop - adjustionHeight) > frameHeight) { //enough space above HREF
            adjustionHeight = (adjustionHeight > 40) ? adjustionHeight/2 : adjustionHeight;
            eTop = eTop - frameHeight - adjustionHeight;
        }
        else if (frameHeight > bodyHeight) { //browser window is shorter than the pop-up layer
            eTop = scrollBarTop;
        }
        else {
            eTop = bodyHeight - frameHeight + scrollBarTop;
        }
    }

    var marginTop = position.top - eTop;
    var marginLeft = position.left - eLeft;
    if ((marginTop > 0) && (marginTop < frameHeight) && (marginLeft > 0) && (marginLeft < frameWidth)) {
        this.redirectSrcElement();
    }
    this.changePosition(eTop, eLeft);
};

popUpPrice.prototype.redirectSrcElement = function() {
    this.backupOnmouseout = this.srcElement.onmouseout;
    this.srcElement.onmouseout = null;
    var oldFrameOnmouseout = this.frameElement.contentWindow.document.onmouseout;
    var obj = this;
    this.frameElement.contentWindow.document.onmouseout = function(){
        if (oldFrameOnmouseout) {
            oldFrameOnmouseout();
        }
        obj.srcElement.onmouseout = obj.backupOnmouseout;
        obj.srcElement.onmouseout();
        obj.frameElement.contentWindow.document.onmouseout = oldFrameOnmouseout;
    }
};

/**
 * Gets offset of pop-ups when adjustion is needed.
 * @param element
*/
popUpPrice.prototype.getAdjustion = function(element) {
    var adjustion = {'width' : 10, 'height' : 10};
    while (element != null && 'TD' != element.nodeName) {
        element = element.parentNode;
    }
    if (element != null) {
        adjustion.width = element.offsetWidth;
        adjustion.height = element.offsetHeight;
    }
    return adjustion;
}

/**
 * Sets offset of pop-ups when adjustion is needed.
 * @param adjustWidth
 * @param adjustHeight
*/
popUpPrice.prototype.setAdjustion = function(adjustWidth, adjustHeight) {
    var _adjustion = {'width' : 0, 'height' : 0};

    _adjustion.width = adjustWidth;
    _adjustion.height = adjustHeight;
    this.adjustion = _adjustion;
}

/**
 * Sets the src element
 * @param element
 */
popUpPrice.prototype.setSrcElement = function(element) {
    if ('A' != element.nodeName) {
        element = element.parentNode;
    }
    this.srcElement = element;
    this.adjustion = this.getAdjustion(element);
};